package com.hmdp.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.hmdp.dto.Result;
import com.hmdp.entity.Follow;
import com.hmdp.mapper.FollowMapper;
import com.hmdp.service.IFollowService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.hmdp.utils.UserHolder;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.data.redis.core.ZSetOperations;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Set;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author 虎哥
 * @since 2021-12-22
 */
@Service
public class FollowServiceImpl extends ServiceImpl<FollowMapper, Follow> implements IFollowService {

    @Autowired
    private StringRedisTemplate stringRedisTemplate;

    /**
     * 关注和取关
     * @param followUserId
     * @param isFollow
     * @return
     */
    @Override
    public Result follow(Long followUserId, Boolean isFollow) {
        // 根据isFollow判断是否关注
        Long userId = UserHolder.getUser().getId();
        if (isFollow) {
            // 关注，数据库新增
            Follow follow = new Follow();
            follow.setUserId(userId);
            follow.setFollowUserId(followUserId);
            this.save(follow);
        }
        // 取关，数据库删除 delete from follow where user_id = ? and follow_user_id = ?
        this.remove(new QueryWrapper<Follow>()
                .eq("user_id",userId).eq("follow_user_id",followUserId));
        return Result.ok();
    }

    /**
     * 查询是否关注
     * @param followUserId
     * @return
     */
    @Override
    public Result followOrNot(Long followUserId) {
        // 查询，select * from follow where user_id = ? and follow_user_id = ?
        Long userId = UserHolder.getUser().getId();
        Follow follow = this.getOne(new QueryWrapper<Follow>()
                .eq("user_id",userId).eq("follow_user_id",followUserId));
        return Result.ok(follow);
    }



}
